﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DMS.Base;
using DMS.BaseClass;
using DMS.Business.Modules.Organization;
using DMS.Business.Utility;
using DMS.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DMS.Business.Modules.ArchivesManagement
{
    public class AttachmentService
    {
        public static AM_ArchivesInfo GetArchInfoById(string archInfoId)
        {
            using (var db = new DMSEntities())
            {
                return db.AM_ArchivesInfo.Where(p => p.ArchivesID == archInfoId).FirstOrDefault();
            }
        }

        /// <summary>
        /// 查询基本信息
        /// </summary>
        /// <param name="actor">参与者，如果为空则不过滤权限</param>
        /// <param name="condition">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页条数，如果小于等于0，则返回所有</param>
        /// <param name="total">总数</param>
        /// <returns></returns>
        public static List<AM_Attachment> GetAttachments(Actor actor, string condition, int pageIndex, int pageSize,
                                                     out int total)
        {
            using (var db = new DMSEntities())
            {
                ////权限过滤
                //condition += GetLimitCondition(actor);

                int skinCount = (pageIndex - 1)*pageSize;

                var q = db.AM_Attachment
                    .Where("it.IsDeleted=false " + condition).OrderByDescending(p => p.CreateTime);

                total = q.Count();

                if (skinCount > total && pageSize > 0)
                {
                    skinCount = total - (total%pageSize);
                }

                List<AM_Attachment> list;
                if (pageSize > 0)
                    list = q.Skip(skinCount).Take(pageSize).ToList();
                else
                    list = q.ToList();

                return list;
            }
        }

        public static void SaveOrUpdateAttachment(AM_Attachment attachment)
        {
            using (var db = new DMSEntities())
            {
                var tAttachment = db.AM_Attachment.FirstOrDefault(p => p.AttachID == attachment.AttachID);
                if (tAttachment != null) //更新
                {
                    db.ApplyCurrentValues("AM_Attachment", attachment);
                }
                else //新增
                {
                    attachment.IsDeleted = false;
                    db.AM_Attachment.AddObject(attachment);
                }
                db.SaveChanges();
            }
        }

        public static void SaveAttachmentList(List<AM_Attachment> attachmentList)
        {
            using (var db = new DMSEntities())
            {
                foreach(var attachment in attachmentList)
                {
                    db.AM_Attachment.AddObject(attachment);
                }
                db.SaveChanges();
            }
        }

        public static void DeleteAttachInfo(string attachId)
        {
            using (var db = new DMSEntities())
            {
                var attach = db.AM_Attachment.Where(p => p.AttachID == attachId).FirstOrDefault();
                if (attach != null)
                {
                    attach.IsDeleted = true;
                    db.SaveChanges();
                }
            }
        }

        public static bool IsUnitNameExist(string unitName,string type,string baseInfoId)
        {
            using (var db = new DMSEntities())
            {
                var count =
                    db.AM_BaseInfo.Where(p => p.UnitName == unitName && p.Type == type && p.BaseInfoID != baseInfoId).
                        Count();
                return count > 0;
            }
        }

        public static bool IsTaxCodeExist(string taxCode,string type,string baseInfoId)
        {
            using (var db = new DMSEntities())
            {
                var count =
                    db.AM_BaseInfo.Where(p => p.TaxCode == taxCode && p.Type == type && p.BaseInfoID != baseInfoId).
                        Count();
                return count > 0;
            }
        }

        public static bool IsInsuranceCodeExist(string insuranceCode,string baseInfoId)
        {
            using (var db = new DMSEntities())
            {
                var count =
                    db.AM_BaseInfo.Where(p => p.TaxCode == insuranceCode && p.BaseInfoID != baseInfoId).
                        Count();
                return count > 0;
            }
        }

    }
}
